package com.cold.demo.jsoup;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author: zyc
 */
public class JsoupUtils {

    public static void main(String[] args) throws IOException {
        /*URL url = new URL("https://www.dianping.com/");
        Consumer<Document> consumer = (document) -> {
            Elements elementsByClass = document.getElementsByClass("first-cate J-primary-menu");
            Elements lis             = elementsByClass.get(0).getElementsByTag("li");
            System.out.println();
        };*/


        URL url = new URL("https://ssl.zc.qq.com/v3/index-chs.html?type=3");
        Consumer<Document> consumer = (document) -> {
            System.out.println(document);
        };

        Connection con = HttpConnection.connect(url);
        String headerStr = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
                "Accept-Encoding: gzip, deflate, br\n" +
                "Accept-Language: zh-CN,zh;q=0.9\n" +
                "Cache-Control: max-age=0\n" +
                "Connection: keep-alive\n" +
                "Cookie: fspop=test; cy=8; cye=chengdu; _lxsdk_cuid=1748a993f8dc8-0b4cf60935057d-3323766-1fa400-1748a993f8dc8; _lxsdk=1748a993f8dc8-0b4cf60935057d-3323766-1fa400-1748a993f8dc8; _hc.v=0418d1eb-696e-303c-8a85-cfd751761eef.1600053133; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1600053133,1600053252; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1600053258; _lxsdk_s=1748a993f8e-cec-994-295%7C%7C22\n" +
                "Host: www.dianping.com\n" +
                "Sec-Fetch-Dest: document\n" +
                "Sec-Fetch-Mode: navigate\n" +
                "Sec-Fetch-Site: cross-site\n" +
                "Sec-Fetch-User: ?1\n" +
                "Upgrade-Insecure-Requests: 1\n" +
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36";
        Map<String, String> headerMap = Arrays.stream(headerStr.split("\n"))
                .filter(str -> (str != null && str.length() > 0)).map(String::trim)
                .collect(Collectors.toMap(str -> str.split(": ")[0], str -> str.split(": ")[1]));

        con.headers(headerMap);

        con.timeout(6000);
        Document document = con.get();

        consumer.accept(document);

    }
}
